<?php
class PostsController extends Controller
{
	public $layout = 'main';

	public function actionIndex()
	{
		//get params
		$strTaxonomyCode =  Yii::app()->request->getParam('type', null);
		$intPage 		= Yii::app()->request->getParam('page', 1);
		$intRowsPerPage = Yii::app()->request->getParam('rpp', MIN_ROWS_PER_PAGE);
		$intSortType 	= Yii::app()->request->getParam('sort',-1);
		
		//query posts
		$args = array(
			'taxonomy_id' => $strTaxonomyCode=='page'?1:2,
			'category_id' => Yii::app()->request->getParam('cat', null)
		);
		$arrArticles = Posts::getInstance()->getList($args, $intPage, $intRowsPerPage);
		$intTotalRows = isset($arrArticles[0]['total_rows'])?$arrArticles[0]['total_rows']:0;
		
		//pagination
		$objPages=new CPagination($intTotalRows);
		$objPages->setCurrentPage($intPage-1);
		$objPages->setPageSize($intRowsPerPage);
		
		//render view
		if($strTaxonomyCode == 'page'){
			$this->render('index-page', array(
				'arrArticles' => $arrArticles,
				'objPages' => $objPages
			));
		} else {
			$arrCategories = Categories::getInstance()->getByParent();
			$this->render('index', array(
				'arrArticles' => $arrArticles,
				'objPages' => $objPages,
				'arrCategories' => $arrCategories
			));
		}
	}
	
	public function actionForm()
	{
		$intPostId = Yii::app()->request->getParam('id', null);
		$strTaxonomyCode =  Yii::app()->request->getParam('type', 'post');
		//update
		if(Yii::app()->request->isPostRequest)
		{
			//get param
			$arrParams = Yii::app()->request->getParam('post', null); 
			$arrParams['post_published_date'] = !empty($arrParams['post_published_date'])?@date('Y-m-d H:i:s', strtotime($arrParams['post_published_date'])):null;
			$arrParams['post_expiry_date'] = !empty($arrParams['post_expiry_date'])?@date('Y-m-d H:i:s', strtotime($arrParams['post_expiry_date'])):null;

			//insert/update article
			if(empty($intPostId)){
				$intResult = Posts::getInstance()->insert($arrParams);
				$intPostId = $intResult;
			}
			else{
				$intResult = Posts::getInstance()->update($intPostId, $arrParams);
			}

			if($intResult > 0){
				//udpate category
				$arrCategoryIds = Yii::app()->request->getParam('category', null); 
				if(!empty($arrCategoryIds)){
					Posts::getInstance()->updateCategory($intPostId, $arrCategoryIds);
				}
				//update tags
				$strTags = Yii::app()->request->getParam('tags', null);
				if(!empty($strTags)){
					Posts::getInstance()->updateTags($intPostId, $strTags);
				}
				//update custom fields
				$arrFields = Yii::app()->request->getParam('fields', null);
				if(is_array($arrFields)){
					foreach($arrFields as $intFieldId => $strFieldValue){
						$strFieldValue = trim($strFieldValue);
						if(!empty($intFieldId)){
							Posts::getInstance()->updateField($intPostId, $intFieldId, $strFieldValue);
						}
					} 					
				}
				Yii::app()->admin->setFlash('ok', "Updated successfully!");
				$this->refresh();
			}
		}
		//load form
		$arrItemInfo = array();
		if(!empty($intPostId)){
			$arrItemInfo = Posts::getInstance()->get($intPostId);
			$arrItemInfo['post_category_id'] = !empty($arrItemInfo['post_category_id'])?explode(',', $arrItemInfo['post_category_id']):array();
			$strTaxonomyCode = $arrItemInfo['taxonomy_code'];
		}
		$arrCustomFields = Posts::getInstance()->getFieldsByTaxonomy($strTaxonomyCode);
		//render layout
		if($strTaxonomyCode == 'page'){
			$this->render('form-page', array(
				'arrItemInfo' => $arrItemInfo
			));
		} else {
			$arrCategories = Categories::getInstance()->getByParent();
			$this->render('form', array(
				'arrItemInfo' => $arrItemInfo,
				'arrCategories' => $arrCategories,
				'arrCustomFields' => $arrCustomFields
			));
		}
	}
	
	public function actionDelete(){
		$intPostId = Yii::app()->request->getParam('id', null);
		if(!empty($intPostId))
				$result = Posts::getInstance()->delete($intPostId);
		header('Location: '.LINK_ADMINCP_ARTICLES);
	}

}